<?php
namespace App\Models;

/**
 * 会员提现日志类
 */
class MWithdrawLog extends BaseModel
{
    public $table = "withdraw_logs";

    public static $STATUS_WAIT = 0; //待审核状态
    public static $STATUS_SUCCESS = 1;  //提现被通过
    public static $STATUS_DENY = 2; //提现被拒绝

    public static $PAY_TYPE_ZFB = 1;    //支付宝
    public static $PAY_TYPE_WX = 2;     //微信
    public static $PAY_TYPE_YHK = 3;    //银行卡

    /**
     * 用户对象 n:1
     */
    public function user()
    {
        return $this->belongsTo(MUser::class, "uid", "id");
    }

    /**
     * 会员提现数据效验规则
     * $flag 0 = rules  1 = msg
     */
//    public static function withdrawRulMsg($flag)
//    {
//        $rules = array(
//            "wallet"   => "required|numeric",
//            'money'    => 'required|numeric|between:1,999999999',
//            'intro'    => 'required|between:1,255',
//        );
//
//        $msg = array(
//            "wallet.required"   => "请选择提现钱包！",
//            "money.required"    => "请输入提现金额！",
//            "intro.required"    => "请必须填写收款备注！",
//        );
//
//        if ($flag == 0) {
//            return $rules;
//        } else {
//            return $msg;
//        }
//    }

    /**
     * 生成一条提现申请
     * $wallet = 钱包类型
     * $money = 提现金额
     * $intro = 提现备注信息
     */

    public static function widthdraw($request_wallet, $request_money, $request, $me, $request_tel, $request_remark)
    {

        $log = new MWithdrawLog();
        $log->no = onlyNo();
        $log->uid = $me->id;
        $log->username = $me->username;
        $log->status = self::$STATUS_WAIT;  //处于等待状态

        $type = intval($request->type); //申请提现类型
        $log->type = $type;

        if ($type == MWithdrawLog::$PAY_TYPE_ZFB) {
            $log->zfb = $request->alipay_no;


        } elseif ($type == MWithdrawLog::$PAY_TYPE_WX) {
            $log->wx = $request->wechat_no;

        } elseif ($type == MWithdrawLog::$PAY_TYPE_YHK) {
            $log->card_name = $request->card_name;
            $log->card_no = $request->card_no;  //卡号
            $log->card_bank = $request->card_bank;

        }

        $log->tel = $request_tel;
        $log->wallet = $request_wallet; //钱包类型
        $log->money = $request_money;
        //计算提现手续费
        $tax_money = (cache('kv_withdraw_tax') / 100) * ($request_money);
        $ok_money = ($request_money) - $tax_money;
        $log->tax_money = $tax_money;
        $log->ok_money = $ok_money;

        $log->remark = $request_remark;//留言
        $log->create_time = time();
        return $log->save() && MConsumeLog::log($me->id, $me->username, $request_money, MConsumeLog::$CONSUME_DEDUCTING,
            $request_wallet, "申请提现，扣除" . uWalletStr($request_wallet) . " $request_money 元！");
    }

//    public static function widthdraw($uid, $username, $wallet, $money, $intro = "")
//    {
//        $log = new MWithdrawLog();
//        $log->no = onlyNo();
//        $log->uid = $uid;
//        $log->username = $username;
//        $log->wallet = $wallet;
//        $log->money = $money;
//
//        //计算提现手续费
//        $tax_money = (cache('kv_tax_money') / 100) * $money;
//        $ok_money = $money - $tax_money;
//        $log->tax_money = $tax_money;
//        $log->ok_money = $ok_money;
//
//        $log->status = self::$STATUS_WAIT;  //处于等待状态
//        $log->intro = $intro;
//        $log->time = time();
//        return $log->save();
//    }

    /**
     * 通过提现申请
     * $result = 打款提醒
     */
    public function allow($result)
    {
        $this->status = MWithdrawLog::$STATUS_SUCCESS;
        $this->result = $result;
        $this->deal_time = time();
        return $this->save();
    }

    /**
     *拒绝提现
     * $result 拒绝理由
     */
    public function deny($result)
    {
        //修改状态并补充拒绝缘由
        $this->status = 2;
        $this->result = $result;

        //退款提现金额
        $user = $this->user;
        $wallet = $user->wallet;
        if ($this->wallet == MWallet::$WALLET_TYPE_1) {
            $wallet->w1 += $this->money;
        } elseif ($this->wallet == MWallet::$WALLET_TYPE_2) {
            $wallet->w2 += $this->money;
        } elseif ($this->wallet == MWallet::$WALLET_TYPE_3) {
            $wallet->w3 += $this->money;
        }

        return $this->save() && $wallet->save() && MConsumeLog::log($user->id, $user->username, $this->money, MConsumeLog::$CONSUME_RECHARGE,
            $this->wallet, "编号为 {$this->no} 的提现申请被拒绝，返还" . uWalletStr($this->wallet) . " $this->money 元！");
    }
}